package com.cms.bs.app.model;

import com.avaje.ebean.Ebean;
import com.cms.bs.app.model.base.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jiang on 2016-09-21.
 */
@Entity
@Table(name = "s_user")
public class User extends BaseEntity<User> {
    @Id
    private long id;
    @Column(updatable = false)
    private String name;
    private String password;
    private String nickname;
    private int gender;
    private int status;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date logintime;
    // 角色列表
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "s_user_role", joinColumns = {@JoinColumn(name = "userId", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "roleId", referencedColumnName = "id")})
    private List<Role> roles = new ArrayList<Role>();

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Date getLogintime() {
        return logintime;
    }

    public void setLogintime(Date logintime) {
        this.logintime = logintime;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", nickname='" + nickname + '\'' +
                ", gender=" + gender +
                ", status=" + status +
                ", logintime=" + logintime +
                ", roles=" + roles +
                '}';
    }

    public static User getByName(String name) {
        return Ebean.find(User.class).where().eq("name", name).findUnique();
    }
}
